package gamagora.game.states
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import gamagora.game.entity.OSBird;
	import gamagora.game.entity.OSBlurEffect;
	import gamagora.game.entity.OSMonkey;
	import gamagora.game.entity.OSSari;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	
	public class OSIntroState extends FlxState
	{
		private static const SPEED:Number = 75.0;
		private static const START_X:Number = -50.0;
		private static const MAX_DRINK:uint = 3;
		private static const TIME_BEFORE_WALK_SOUND:Number = 0.8;
		private static const MOVE:String = "move";
		private static const START_DRINK:String = "start drink";
		private static const DRINK:String = "drink";
		private static const ARM_UP:String = "arm up";
		
		private var _blackFade:FlxSprite;
		private var _woman:FlxSprite;
		private var _monkey:OSMonkey;
		private var _completeFade:Boolean;
		private var _stopped:Boolean;
		private var _startDrink:Boolean;
		private var _nbDrinks:uint;
		private var _end:Boolean;
		private var _goToPS:Boolean;
		private var _sari:OSSari;
		private var _monkeyMove:Boolean;
		private var _cutScene:Boolean;
		private var _birdFlight:Boolean;
		private var _timeBeforeWalkSound:Number;
		
		private var _flBird1:OSBird;
		private var _flBird2:OSBird;
		private var _flBird3:OSBird;
		
		public override function create():void
		{
			add(new FlxSprite(0, 0, OSImages.BG));
			add(new FlxSprite(0, 0, OSImages.INTRO_BACKGROUND));
			
			//Woman
			_timeBeforeWalkSound = 0;
			_woman = new FlxSprite(START_X, 404);
			_woman.offset.x = 58;
			_woman.scale.x = _woman.scale.y = 1.5;
			_woman.loadGraphic(OSImages.WOMAN, true, false, 128, 128);
			_woman.addAnimation(MOVE,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20);
			_woman.addAnimation(START_DRINK,[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34], 20, false);
			_woman.addAnimation(DRINK,[35, 36, 37, 38, 39, 38, 37, 36, 35], 10);
			_woman.addAnimation(ARM_UP, [36, 37, 38, 39, 40, 41, 42, 43, 44], 15, false);
			_woman.play(MOVE);
			add(_woman);
			
			//Monkey
			_monkey = new OSMonkey(START_X - 50, 464, false, 0, 30, true);
			_monkey.animOnly = true;
			_monkey.canCry = false;
			_monkey.facing = FlxObject.LEFT;
			_monkey.play(OSMonkey.RUN);
			add(_monkey);
			
			//Birds
			add(new OSBird(52, 80, true, false, 20, true));
			add(new OSBird(244, 174, true, false, 20, true));
			add(new OSBird(534, 234, true, false, 20, true));
			add(new OSBird(710, 218, false, false, 20, true));
			
			_flBird1 = new OSBird(500, 500, false, false, 20, true);
			_flBird2 = new OSBird(540, 500, false, false, 20, true);
			_flBird3 = new OSBird(610, 430, false, false, 20, true);
			add(_flBird1);
			add(_flBird2);
			add(_flBird3);
			
			//Black fade
			_blackFade = new FlxSprite();
			_blackFade.makeGraphic(FlxG.width, FlxG.height, 0xff000000);
			add(_blackFade);
			
			//Blur effects
			/*
			var effect:OSBlurEffect = new OSBlurEffect(660, 480, 250, 1);
			effect.setXSpeed(-5, 5);
			effect.setYSpeed(-20, -60);
			effect.effectBlobHeight = 10;
			effect.effectBlobWidth = 6;
			effect.effectDisplaceAmtX = 2;
			effect.effectDisplaceAmtY = 3;
			effect.effectAlpha = .6;
			effect.effectLife = 0.5;
			effect.effectBlobCount = 200;
			effect.setStartX(0);
			add(effect);
			*/
			
			//Sari
			_sari = new OSSari(616, 426);
			_sari.moving = false;
			_sari.angle = 120;
			_sari.visible = false;
			add(_sari);
		}
		
		public override function update():void
		{
			super.update();
			
			if(!_cutScene)
			{
				if(FlxG.keys.any())
				{
					FlxG.fade(0xff000000, 2, goToPlayState);
					_cutScene = true;	
				}
				
				if(!_completeFade)
				{
					_blackFade.alpha -= FlxG.elapsed;
					if(_blackFade.alpha <= 0)
					{
						_completeFade = true;
					}
				}
				else
				{
					if(_stopped)
					{
						if(!_startDrink)
						{
							if(_woman.finished)
							{
								OSSounds.playWind(0.2);
								_woman.play(DRINK);
								_startDrink = true;
							}
						}
						else
						{
							if(!_end)
							{
								if(_nbDrinks >= MAX_DRINK)
								{
									_woman.play(ARM_UP);
									_monkey.play(OSMonkey.START_RUN);
									_end = true;
								}
								else
								{
									if(_woman.finished)
									{
										_nbDrinks++;
										_woman.play(DRINK, true);
									}
								}
							}
							else
							{
								if(!_sari.visible)
								{
									if(_woman.frame == 41)
									{
										_sari.moving = true;
										_sari.visible = true;
										_monkeyMove = true;
										OSSounds.playWomanCry();
									}
								}
								
								if(_monkeyMove)
								{
									if(_monkey.x <= 1050)
									{
										_monkey.x += FlxG.elapsed * SPEED * 2.5;
									}
									else
									{
										if(!_goToPS)
										{
											FlxG.fade(0xff000000, 3, goToPlayState);
											_goToPS = true;
										}
									}
								}
							}
						}
					}
					else
					{
						if(_woman.x < 600)
						{
							_woman.x += FlxG.elapsed * SPEED;
							_monkey.x += FlxG.elapsed * SPEED;
							
							_timeBeforeWalkSound += FlxG.elapsed;
							if(_timeBeforeWalkSound >= TIME_BEFORE_WALK_SOUND)
							{
								OSSounds.playWomanStep();
								_timeBeforeWalkSound = 0;
							}
							
							if(_woman.x >= 600)
							{
								_stopped = true;
								_woman.play(START_DRINK);
								_monkey.play(OSMonkey.STOP_RUN);
							}
							else
							{
								if(_woman.x >= 300 && !_birdFlight)
								{
									_flBird1.play(OSBird.START_FLY);
									_flBird2.play(OSBird.START_FLY);
									_flBird3.play(OSBird.START_FLY);
									_birdFlight = true;
								}
							}
						}
					}
				}
			}
		}
		
		private function goToPlayState():void
		{
			FlxG.switchState(new OSPlayState());
		}
	}
}
